--
-- Author: mm
-- Date: 2018-04-05 16:33:25
--
-- LionHornRewardRender
-- 狮心号角奖励节点

local IconRender = require("app.widget.renders.IconRender")

local ClassRef = app.mvc.createRenderClass(...)

--data =
function ClassRef.createWithData(data, node)
    local sprite = ClassRef.extend(node or cc.CSLoader:createNode(Res.LionRoundEndNode))
    if node == nil then
        ccui.Helper:doLayout(sprite)
    end
    sprite:init(data)
    return sprite
end
function ClassRef.create(node)
    return ClassRef.createWithData(nil, node)
end

--
function ClassRef:init(data)
    self.super.init(self, data)
    self:getTimeline():gotoFrameAndPause( 0 )

    self:setContentSize( self._ui.Panel:getContentSize() )
    self._ui.Panel_icon:setVisible( false )
    self._rewardIcon = IconRender.create( self._ui.Panel_icon:getChildByName( "icon" ) )
    self._rewardIcon:setTipEnable( true )

    self:refresh()
end


-- data = message LionHeartHornMonsterInfo{
--     int32 monsterID  = 1;//怪物ID
--     int32 monsterNum = 2;//怪物击杀数量
--     repeated shared.Loot Loots   = 3;//怪物奖励
-- }

-- data = message LionHeartHornMonsterInfo{
--     int32 monsterID  = 1;//怪物ID
--     int32 monsterNum = 2;//怪物击杀数量
--     int32 RewardID   = 3;//怪物掉落ID -- 掉落表ID
-- }
-- isMaxRewardTimes
function ClassRef:refresh()
    if self._data and self._data[1] then
        self:setVisible( true )
        local data = self._data[1]

        local  npcResConf = GD:queryNpcRes( data.monsterID )
        local bossIconPath = GD:getImagePath( Res.npcIconPath, npcResConf.face )
        self._ui.Panel_head:getChildByName( "icon" ):loadTexture( bossIconPath )

        local lionRewardConf = GD:getLionRewardById( data.monsterID )
        local rewardType = tonumber( lionRewardConf.type )
        local rewardDesc = ""
        if 1 == rewardType then -- 普通
            rewardDesc = L("lua_code_text_285")
        elseif 2 == rewardType then -- boss
            rewardDesc = L("lua_code_text_32")
        elseif 3 == rewardType then -- 稀有boss
            rewardDesc = L("lua_code_text_534")
        end
        self._ui.Panel_head:getChildByName( "tips" ):setString( rewardDesc )

        local npcConf = GD:queryNpc( data.monsterID )
        local rewardName = table.concat( { L(npcConf.name), "x", data.monsterNum } )
        self._ui.Panel_head:getChildByName( "name" ):setString( rewardName )

        if not self._data[2] then -- 没有达到最大可领奖次数
            if 1 < #data.Loots then
                print( " ----------->>>>>> 一个怪物击杀对应多个奖励？" )
                return
            elseif 0 >= #data.Loots then
                print( " ----------->>>>>> 没有达到最大可领奖次数，但是服务器没给具体奖励" )
                return
            end

            local dropData = UD:parseLootToBagItem( data.Loots[1] )
            self._rewardIcon:setData( dropData )
            local charNum = app:isLanguageCN() and 9 or 20
            self._ui.Panel_icon:getChildByName( "name" ):setString( dropData:getName(false, charNum) )

            self:performWithDelay(function()
                self:playAction()
            end, 1)
        end
    else
        self:setVisible( false )
    end
end

function ClassRef:playAction()
    self:getTimeline():play( "animation", false )
    self:performWithDelay(function()
        self._ui.Panel_icon:setVisible( true )
    end, 20/60)
end

function ClassRef:onBtnClick(cb)
    self._onClick = cb
end

return ClassRef
